<?php

class Core_Controller_Admin_Appearance_Form extends Controller
{

    public function actionIndex($request, $response)
    {
        $req = $this -> _request;
        $table = Agis::getTable('core@form');
        $select = $table -> select();

        $paginator = $response -> paginator = Zend_Paginator::factory($select);

        $paginator -> setCurrentPageNumber($request -> getParam('page', 1));
    }

    public function actionAdd($request, $response)
    {

    }

    public function actionEdit($request, $response)
    {
        $response -> form = $form = new Core_Form_Admin_Form_Edit;

        $form_id = intval($request -> getParam('form_id', 0));

        $row = Agis::getTable('core@form') -> find($form_id) -> current();

        if ($req -> isGet())
        {

            if (is_object($row))
            {
                $form -> setValues($row -> toArray());
            }
            return;
        }

        if ($request -> isPost() && $form -> isValid($_POST))
        {
            $row -> setFromArray($form -> getValues());

            $row -> save();
            $this -> redirect('admin/appearance/form');
        }

        return;
    }

    public function actionAddModelForm($request, $response)
    {

    }

    /**
     * manage menu items of an menu.
     */
    public function actionElements($request, $response)
    {
        $formId = $req -> getParam('form_id', 1);

        $table = Agis::getTable('core@form.element');
        $select = $table -> select() -> where('form_id=?', $formId) -> order('pid') -> order('priority');

        $paginator = $response -> paginator = Zend_Paginator::factory($select);

    }

    public function actionAddElement($request, $response)
    {
        $form = $response -> form = Agis::getForm('core@admin.form.addelement');

        if ($request -> isPost() && $form -> isValid($_POST))
        {
            $values = $form -> getValues();

            $tableEle = Agis::getTable('core@form.element');
            $row = $tableEle -> fetchNew();
            $row -> setFromArray($values);
            $spec = array(
                'label' => $values['label'],
                'desc' => $values['desc']
            );
            $row -> spec = serialize($spec);
            $row -> save();

            $this -> redirect('admin/appearance/form/element', array('form_id' => $values['form_id']));
        }

    }

    public function actionEditElement($request, $response)
    {

        $id = $request -> getParam('id', 0);

        $row = Agis::getTable('core@form.element') -> find($id) -> current();

        $form = $response -> form = Agis::getForm('core@admin.form.addelement');

        if ($request -> isGet() && is_object($row))
        {
            $spec = $row -> spec ? unserialize($row -> spec) : array();
            $spec = array_merge($row -> toArray(), $spec);
            $form -> setValues($spec);
        }

        if ($request -> isPost() && $form -> isValid($_POST))
        {
            $values = $form -> getValues();

            $row -> setFromArray($values);
            $spec = array(
                'label' => $values['label'],
                'desc' => $values['desc']
            );
            $row -> spec = serialize($spec);
            $row -> save();

            $this -> redirect('admin/appearance/form/element', array('form_id' => $values['form_id']));
        }

    }

}
